home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / B-C / CPictureButton.sit / CPictureButtonƒ / CPictureButton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-27  |  6.5 KB  |  267 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  CPictureButton.c
  3.  
  4.  
  5.  ******************************************************************************/
  6.  
  7. #include "CPictureButton.h"
  8. #include "OSChecks.h"
  9. #include "Commands.h"
  10. #include "Global.h"
  11.  
  12.  
  13. /******************************************************************************
  14.  IPictureButton
  15.  
  16.      Initialize an PictureButton. The pictures are taken from the specified pict
  17.      id resources. You MUST specify pict ids for the standard, disabled, and pressed
  18.      pict resources. 
  19.      
  20.      Additionally, you will need to know the width and height of the picture
  21.      button's pict resources. 
  22. ******************************************************************************/
  23.  
  24. void CPictureButton::IPictureButton( CView *anEnclosure, CBureaucrat *aSupervisor,
  25.                     short aHEncl, short aVEncl,short width,  short height,
  26.                     SizingOption aHSizing, SizingOption aVSizing,
  27.                     short pictID,  short disabled, short pressed)
  28. {    
  29.     CPicture::IPicture( anEnclosure, aSupervisor,width,height, aHEncl, aVEncl,
  30.                 aHSizing, aVSizing);
  31.     
  32.     this->pictID = pictID;
  33.     this->disabledID = disabled;
  34.     this->pressedID = pressed;
  35.     pictH = NULL;
  36.     clickCmd = cmdNull;
  37.     disabledPicH = NULL;
  38.     pressedPicH = NULL;
  39.     active = TRUE;
  40.     IPictureButtonX();
  41. }    /* CPictureButton::IPictureButton */
  42.  
  43. void    CPictureButton::IPictureButtonX(){
  44.     LongRect    lRect;
  45.     
  46.     pictH = GetPicture(pictID);
  47.     disabledPicH = GetPicture(disabledID);
  48.     pressedPicH = GetPicture(pressedID);
  49.     UsePICT(pictID);
  50.     SetWantsClicks(active);
  51.     FrameToBounds();
  52.     
  53. }
  54.  
  55. void    CPictureButton::Activate() {
  56.     if(!active) {
  57.         active = TRUE;
  58.         SetWantsClicks(TRUE);
  59.         DrawPict(FALSE);
  60.         
  61.     }
  62.     inherited::Activate();
  63.  
  64. }
  65. void    CPictureButton::Deactivate() {
  66.     
  67.     if(active) {
  68.         active = FALSE;
  69.         SetWantsClicks(FALSE);
  70.         DrawPict(FALSE);
  71.     }
  72.     inherited::Deactivate();
  73.  
  74. }
  75.  
  76.  
  77. /******************************************************************************
  78.  DrawPict
  79.      Prepare sets up the coordinate system, and macPort.
  80.      Draw draws the correct pict id in the frame.
  81. ******************************************************************************/
  82.  
  83. void CPictureButton::DrawPict( Boolean fHilite)
  84. {
  85.     RGBColor    fore, back;
  86.     Rect        qdRect;    
  87.     
  88.     Prepare();
  89.     if (macPicture && !fHilite) {
  90.         active ? UsePICT(pictID) : UsePICT(disabledID);
  91.         LongToQDRect( &bounds, &qdRect)    ;
  92.         //FrameToBounds();
  93.         inherited::Draw(&qdRect);
  94.     }
  95.     else if(fHilite) {
  96.         UsePICT(pressedID);
  97.         LongToQDRect( &bounds, &qdRect)    ;
  98.         //FrameToBounds();
  99.         inherited::Draw(&qdRect);
  100.     }
  101.  
  102. }    /* CPictureButton::DrawPicture */
  103.  
  104. /******************************************************************************
  105.  Draw
  106.  
  107.      Draw the pict in response to an update.
  108. ******************************************************************************/
  109.  
  110. void  CPictureButton::Draw( Rect *area)
  111. {
  112.     DrawPict( FALSE);
  113.  
  114. }    /* CPictureButton::Draw */
  115.  
  116.  
  117. /******************************************************************************
  118.  SetClickCmd
  119.  
  120.      Set the clickCmd for an PictureButton. This command will be sent to
  121.      itsSupervisor when the picture is clicked and the mouse is released
  122.      inside the picture.
  123. ******************************************************************************/
  124.  
  125. void CPictureButton::SetClickCmd( long aCmd)
  126. {
  127.     clickCmd = aCmd;
  128.  
  129. }    /* CPictureButton::SetClickCmd */
  130.  
  131. /******************************************************************************
  132.  GetClickCmd
  133.  
  134.      Return a CPictureButton's clickCmd
  135. ******************************************************************************/
  136. long CPictureButton::GetClickCmd( void)
  137. {
  138.     return clickCmd;
  139.  
  140. }    /* CPictureButton::GetClickCmd */
  141.  
  142. /******************************************************************************
  143.  DoClick
  144.  
  145.      Respond to a click. The wantsClicks instance variable must be
  146.      TRUE for this method ever to be called.
  147. ******************************************************************************/
  148.  
  149. void CPictureButton::DoClick( Point hitPt, short modifierKeys, long when)
  150. {
  151.     if (Track())
  152.     {
  153.         itsSupervisor->DoCommand( GetClickCmd());
  154.     }
  155.  
  156. }    /* CPictureButton::DoClick */
  157.  
  158. /******************************************************************************
  159.  Track
  160.  
  161.      Tracks the mouse, returns TRUE if the mouse was in the Picture when released.
  162. ******************************************************************************/
  163.  
  164. Boolean CPictureButton::Track( void)
  165. {
  166.     Boolean    inBtn = TRUE;
  167.     Point    where;
  168.     Rect    qdFrame;
  169.         
  170.     DrawPict( TRUE);
  171.     LongToQDRect( &frame, &qdFrame);
  172.     
  173.     while (StillDown())
  174.     {
  175.         GetMouse( &where);
  176.         if (PtInRect( where, &qdFrame))
  177.         {
  178.             if (!inBtn) DrawPict( TRUE);
  179.             inBtn = TRUE;
  180.         }
  181.         else
  182.         {
  183.             if (inBtn) DrawPict( FALSE);
  184.             inBtn = FALSE;
  185.         }
  186.     }
  187.     if (inBtn) DrawPict( FALSE);
  188.     return inBtn;
  189.  
  190. }    /* CPictureButton::Track */
  191.  
  192. /******************************************************************************
  193.  Dispose
  194.  
  195. ******************************************************************************/
  196.  
  197. void  CPictureButton::Dispose( void)
  198. {
  199.  
  200. }    /* CPictureButton::Dispose */
  201.  
  202.  
  203.  
  204. /****        This code is based on the Object I/O code for saving pictures         ***/
  205. /****    If you do not have Object I/O, you can remove everything from here down ***/
  206. /****    Remember to remove the header prototypes for these routines, too        ***/
  207.  
  208. /****   ------------------* Object I/O *------------------  ****/
  209. /****    Copyright © 1990, 1992 Object Factory Incorporated    ****/
  210. /****                    All rights reserved                    ****/
  211.  
  212. typedef struct {
  213.     short        pictID;
  214.     short        disabledID;
  215.     short        pressedID;
  216.     long        clickCmd;
  217.     Boolean        active;
  218. } SavePictureButton;
  219.  
  220.  
  221.  
  222.  
  223. /******************************************************************************
  224.  PutTo
  225.         Object I/O based code, for saving the picture button to a Stream.
  226.         Put the contents of this object to the stream
  227.  ******************************************************************************/
  228.  
  229. void    CPictureButton::PutTo(
  230.     CStream        *aStream)
  231. {
  232.     SavePictureButton    s;
  233.  
  234.     s.pictID = pictID;
  235.     s.disabledID = disabledID;
  236.     s.pressedID = pressedID;
  237.     s.clickCmd = clickCmd;
  238.     s.active = active;
  239.     aStream->PutStruct(s);
  240.     inherited::PutTo(aStream);
  241. }
  242.  
  243.  
  244. /******************************************************************************
  245.  GetFrom
  246.         Object I/O based code, for getting the picture button from a Stream
  247.  
  248.         Get the contents of this object from the stream and
  249.         initialize the object
  250.  ******************************************************************************/
  251.  
  252. void    CPictureButton::GetFrom(
  253.     CStream     *aStream)
  254. {
  255.     SavePictureButton    s;
  256.  
  257.     aStream->GetStruct(s);
  258.     pictID = s.pictID;
  259.     pressedID = s.pressedID;
  260.     disabledID = s.disabledID;
  261.     clickCmd = s.clickCmd;
  262.     active = s.active;
  263.     inherited::GetFrom(aStream);
  264.     IPictureButtonX();
  265. }
  266.  
  267.